//
//  PickerDateView.m
//  text
//
//  Created by com.chetuba on 15/6/6.
//  Copyright (c) 2015年 LXTX. All rights reserved.
//

#import "XBPickerDateView.h"


@interface XBPickerDateView ()

///视图
@property (nonatomic, strong) UIView *mView;
///黑色背景
@property (nonatomic, strong) UIView *mTableBaseView;
///时间
@property (nonatomic,strong) NSDate *date;

@end

@implementation XBPickerDateView




- (instancetype)initWithFrame:(CGRect)frame delegate:(id<XBPickerDateViewDelegate>)delegate{
    if (self = [super initWithFrame:frame]) {
        
        self.delegate = delegate;
        
        self.mTableBaseView = [[UIView alloc] initWithFrame:frame];
        self.mTableBaseView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.5];
        
        UITapGestureRecognizer *bgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgTappedAction:)];
        [self.mTableBaseView addGestureRecognizer:bgTap];
        
        [self addSubview:self.mTableBaseView];
        
        self.mView = [[UIView alloc] initWithFrame:CGRectMake(0, frame.size.height-224, frame.size.width, 224)];
        self.mView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.mView];
        
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 44)];
        imageView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self.mView addSubview:imageView];
        
        self.datePickers=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 44, self.mView.frame.size.width, self.mView.frame.size.height-44)];
        self.datePickers.datePickerMode = UIDatePickerModeDate;
        [self.datePickers addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
        [self.mView addSubview:self.datePickers];
        
        UIButton *ok=[[UIButton alloc]initWithFrame:CGRectMake(8, 2, 40, 40)];
        [ok setTitle:@"取消" forState:UIControlStateNormal];
        [ok setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [ok addTarget:self action:@selector(endButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.mView addSubview:ok];
        
        UIButton *end=[[UIButton alloc]initWithFrame:CGRectMake(frame.size.width-88, 2, 88, 40)];
        [end setTitle:@"确认" forState:UIControlStateNormal];
        [end setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [end addTarget:self action:@selector(okButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.mView addSubview:end];
        
        [self set];
        
    }
    return self;
}

-(void)dateChanged:(id)sender{
    
    NSDate* date1 = self.datePickers.date;
    /*添加你自己响应代码*/
    self.date = date1;
}
/**
 *  点击透明处 缩回动画
 *
 *  @param tap 点击手势
 */
-(void)bgTappedAction:(UITapGestureRecognizer *)tap
{
   
    [self setHidePickerDateView];
}
#pragma mark - Button lifecycle
/**
 *  点击ok
 */
- (void)okButtonClicked{
    [self setHidePickerDateView];
    
    
    if (self.delegate&&[self.delegate respondsToSelector:@selector(pickerDidChaneStatus:picker:)]) {
        
        [self.delegate pickerDidChaneStatus:self.date picker:self];
    }
    
    
}
/**
 *  点击取消

 */
- (void)endButtonClicked{
    [self setHidePickerDateView];

}

- (void)set{
    
//    NSTimeInterval  time=[[NSDate date]timeIntervalSinceDate:[NSDate date]];
//    
//    NSTimeInterval  maxtime = time;   //-(3600*24*1);
//    
//    NSDate *minDate=[[NSDate alloc]initWithTimeIntervalSinceNow:mintime];
//    NSDate *maxDate=[[NSDate alloc]initWithTimeIntervalSinceNow:maxtime];
    

    self.datePickers.minimumDate =   [NSDate date];
//    datePickers.maximumDate =
    ///让他提前
    //datePickers.date=[self getDateOf:[self getDateString:minDate]];
    
    self.datePickers.date= [NSDate date];
    
    NSDate* date1 = self.datePickers.date;
    
    self.date = date1;
    
    
    
    
    
    
}


///通知出现
- (void)showShowPickerDateView:(UIView *)view{
    
    
    if (self.delegate&&[self.delegate respondsToSelector:@selector(pickerDidChaneStatus:picker:)]) {
        
        [self.delegate pickerDidChaneStatus:self.date picker:self];
    }
   
    
    
    self.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [view addSubview:self];
    
    
    self.mView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 224);
    
    self.mTableBaseView.alpha = 0;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        
        self.mView.frame = CGRectMake(0, self.frame.size.height-224, self.frame.size.width, 224);
        
        self.mTableBaseView.alpha = 0.5;
        
    }];
    
}
///通知隐藏

- (void)setHidePickerDateView{
    
    
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.mView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 224);
                         
                         self.mTableBaseView.alpha = 0;
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                         
                     }];

}


- (NSString *)getDateString:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay |  NSCalendarUnitHour | NSCalendarUnitMinute;
    NSDateComponents *components = [calendar components:unitFlags fromDate:date];
    ;
    NSInteger iCurYear= [components year];  //当前的年份
    NSInteger iCurMonth= [components month];  //当前的月份
    NSInteger iCurDay= [components day];    //当前的天数

    
    NSString *TimeSting=[NSString stringWithFormat:@"%zi-%zi-%zi",iCurYear,iCurMonth,iCurDay];
    return TimeSting;
}

- (NSDate *)getDateOf:(NSString *)string{
    NSDateFormatter *formatte=[[NSDateFormatter alloc]init];
    [formatte setDateFormat:@"YYYY-MM-dd"];
    NSDate *Date=[formatte dateFromString:string];
    return Date;
}

@end
